package servlet;

import dao.ImgDao;
import dao.factoty.DaoFactory;
import domain.Img;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet(name = "FindImgById", value = "/FindImgById")
public class FindImgById extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        int id = Integer.parseInt(request.getParameter("id"));
        String url = request.getParameter("url");
        ImgDao imgDao = DaoFactory.getImgDaoInstance();
        int res = -1;
        try{
            res = imgDao.findImgById(id, url);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        request.setAttribute("img", res);
        request.getRequestDispatcher("/ImgListServlet").forward(request, response);
    }
}
